function Block (_pieceType, _up, _right, _down, _left) {

  this.uuid = new UUID();

  this.div = new Element('div', { 'class' : 'block' });//document.createElement('div');
  this.div.setAttribute('uuid', this.uuid);
  this.div.observe('click', this.debugOnClick);

  this.span = new Element('span');
  this.span.setAttribute('uuid', this.uuid);
  this.span.innerHTML = '1, ';
  this.span.observe('mouseover', this.debugOnClick);
 
  objectGlobals.blocks[this.uuid] = this;

  this.xPosition = 0; 
  this.yPosition = 0;  

  this.trans = 1;
    
  this.pieceType = _pieceType;
  this.blockType = Math.round(Math.random() * 4);
  this.rotation = Math.round(Math.random() * 3); 

  this.pipeSize = 0;
  this.pipeId = -1;

  this.cycleId = -1;
  this.cyclePos = Math.round(Math.random() * 6); 

  this.checked = false;
  this.destroying = false;
  this.piping = false;
  this.testCycle = false;
  this.cycling = false;
  this.droppable = false;
  
  this.empty = false;
  
  this.found = new Array();
  this.cfound = new Array();

  this.pieceAssociations = new Array();
  this.pieceAssociations[0] = _up;
  this.pieceAssociations[1] = _right;
  this.pieceAssociations[2] = _down;
  this.pieceAssociations[3] = _left;

  this.associationCopy = new Array();

  this.pipeConnections = new Array();

  switch(this.blockType) {
    case 0 : 
      for (var i = 0; (i < 4); i++) {
        this.pipeConnections[i] = true;
      }
      break;
    case 1:
      this.pipeConnections[0] = true;
      this.pipeConnections[1] = false;
      this.pipeConnections[2] = true;
      this.pipeConnections[3] = false;
      break;
    case 2:
      this.pipeConnections[0] = true;
      this.pipeConnections[1] = true;
      this.pipeConnections[2] = false;
      this.pipeConnections[3] = true;
      break;
    case 3:
      this.pipeConnections[0] = true;
      this.pipeConnections[1] = true;
      this.pipeConnections[2] = false;
      this.pipeConnections[3] = false;
      break;
    case 4:
      this.pipeConnections[0] = true;
      this.pipeConnections[1] = false;
      this.pipeConnections[2] = false;
      this.pipeConnections[3] = false;
      break;
   }

  this.connectionCopy = new Array();
  this.trueConnections = new Array();

  this.links = new Array();
  this.cyclePath = new Array();
  this.cyp = null; 
 
  for (var i = 0; (i < 4); i++) {
    this.found[i] = false;
    this.cfound[i] = false;
    this.links[i] = null;
    this.cyclePath[i] = null;
  }

  for(var i = 0; (i < this.rotation); i++) {
    this.rotateConnections();
  } 

  this.setRotation();
}

Block.prototype.debugOnClick = function(event) {
  debug(dumpObj(objectGlobals.blocks[event.element().getAttribute('uuid')], 'bl', '<br />', 1));

var el = objectGlobals.blocks[event.element().getAttribute('uuid')]; 
 
  if (el.pieceAssociations[0]) {
    el.div.style.borderTop = "2px solid black";
  }

  if (el.pieceAssociations[1]) {
    el.div.style.borderRight = "2px solid black";
  }

  if (el.pieceAssociations[2]) {
    el.div.style.borderBottom = "2px solid black";
  }

  if (el.pieceAssociations[3]) {
    el.div.style.borderLeft = "2px solid orange";
  }


}

Block.prototype.setPosition = function(_xPosition, _yPosition) {
  if (typeof(_xPosition) != 'undefined') { this.xPosition = _xPosition; }
  if (typeof(_yPosition) != 'undefined') { this.yPosition = _yPosition; }

  this.bgp = this.div.style.backgroundPosition;

  this.div.style.left = (this.xPosition * objectGlobals.blockSize) + 'px' ;
  this.div.style.top = (this.yPosition * objectGlobals.blockSize) + 'px';
}

Block.prototype.setRotation = function(_rotation) {

  this.rotation = typeof(_rotation) != 'undefined' ? _rotation : this.rotation;

  var styleX = -(this.blockType * objectGlobals.blockSize);
  var styleY = -(this.rotation * objectGlobals.blockSize);

  this.div.style.backgroundImage = "url(images/piece_" + this.pieceType + ".png)";
//  $('test_div').innerHTML += 'pt: ' + this.pieceType + ' ' + objectGlobals['piece_' + this.pieceType] + "<br />";
  this.div.style.backgroundPosition = styleX + "px " + styleY + "px";

  /*
//pipe connection testing

//  this.div.style.border = "1px solid transparent";

  if (this.pipeConnections[0]) {
    this.div.style.borderTop = "1px solid red";
  }

  if (this.pipeConnections[1]) {
    this.div.style.borderRight = "1px solid blue";
  }

  if (this.pipeConnections[2]) {
    this.div.style.borderBottom = "1px solid black";
  }

  if (this.pipeConnections[3]) {
    this.div.style.borderLeft = "1px solid orange";
  }
*/

}

Block.prototype.rotateLeft = function() {
  this.associationCopy.each(function(ac) { ac = null; });
  this.connectionCopy.each(function(cc) { cc = null; });
 
  this.associationCopy[3] = this.pieceAssociations[0];
  this.connectionCopy[3] = this.pipeConnections[0];

  for(var i = 0; (i < 3); i++) {
    this.associationCopy[i] = this.pieceAssociations[i + 1];
    this.connectionCopy[i] = this.pipeConnections[i + 1];
  }

  for(var i = 0; (i < 4); i++) {
    this.pieceAssociations[i] = this.associationCopy[i];
    this.pipeConnections[i] = this.connectionCopy[i]; 
  }

  this.setRotation(((this.rotation == 0) ? 3 : this.rotation - 1)); 
}

Block.prototype.rotateRight = function() {
  this.associationCopy.each(function(ac) { ac = null; });
  this.connectionCopy.each(function(cc) { cc = null; });

  this.associationCopy[0] = this.pieceAssociations[3];
  this.connectionCopy[0] = this.pipeConnections[3];

  for(var i = 3; (i > 0); i--) {
    this.associationCopy[i] = this.pieceAssociations[i - 1];
    this.connectionCopy[i] = this.pipeConnections[i - 1];
  }

  for(var i = 0; (i < 4); i++) {
    this.pieceAssociations[i] = this.associationCopy[i];
    this.pipeConnections[i] = this.connectionCopy[i]; 
  }

  this.setRotation((this.rotation == 3) ? 0 : this.rotation + 1); 
}

Block.prototype.rotateConnections = function() {
  this.connectionCopy.each(function(cc) { cc = null; });
  this.connectionCopy[0] = this.pipeConnections[3];

  for( var i = 0; (i < 3); i++) {
    this.connectionCopy[i + 1] = this.pipeConnections[i];
  }

  this.pipeConnections = this.connectionCopy.clone();

  for( var i = 0; (i < 4); i++) {
    this.pipeConnections[i] = this.connectionCopy[i];
  }
}

Block.prototype.clearLinks = function() {
  this.links.each(function(link) { link = null; });
}

Block.prototype.debugTrueConnections = function() {
/*
  this.div.style.border = "1px solid transparent";

  if (this.trueConnections[0]) {
    this.div.style.borderTop = "1px solid red";
  }

  if (this.trueConnections[1]) {
    this.div.style.borderRight = "1px solid red";
  }

  if (this.trueConnections[2]) {
    this.div.style.borderBottom = "1px solid red";
  }

  if (this.trueConnections[3]) {
    this.div.style.borderLeft = "1px solid red";
  }
*/
}

Block.prototype.setPiping = function(_pipeId) {
//  window.alert('setPiping' + this.xPosition +  ' ' + this.yPosition);
  this.pipeId = _pipeId;
  this.piping = true;
//  this.div.style.border = '2px solid black';

  var styleX = -(this.blockType * objectGlobals.blockSize);
  var styleY = -(this.rotation * objectGlobals.blockSize);

  this.div.style.backgroundImage = "url(images/piece_9.png)";
  this.div.style.backgroundPosition = styleX + "px " + styleY + "px";

  this.setPosition();  
}
